nodeflagperl = new Array(); // contails all the nodes
for (i = 0; i < 80; i++) {
	nodeflagperl[i] = 1;
	// 1:wait for burning
	// 0:seleted to burn
	// 2:blink
	// 3:done successfully
	// 4:done unsuccessfully
}

// ////////////////////////tianjing//////////////////////////

canvas_height = 320;
canvas_width = 600;
radius = canvas_width / 2;
x_change = new Array();
y_change = new Array();
x_change = [ 1.0, 0.742, 0.395, 0, 0.395, 0.742, 1.0 ];// dui x zuobiao zuo
// zhuan huan
y_change = [ 0.5, 0.766, 0.940, 1, 0.940, 0.766, 0.5 ];

var xynode = new Array(); // contains nodes except sink
var pianyi = 20;

var clear = 1;// mei ji shu dao 5 ,qing 0.

for (var m = 0; m < 35; m++) {
	xynode[m] = new Array();
	colnum = parseInt(m / 5);
	x_changed = parseFloat(x_change[colnum]);
	y_changed = parseFloat(y_change[colnum]);
	if (m < 20) {
		xynode[m][0] = radius * (1 - 0.2 * clear * x_changed) + pianyi;
	} else {
		xynode[m][0] = radius * (1 + 0.2 * clear * x_changed) + pianyi;
	}
	xynode[m][1] = canvas_height - radius * 0.2 * clear * y_changed;
	xynode[m][2] = m + 1 + 1; // NOTE: sink is treat alone; and sink if 1; so
	// other nodes from 2 ~ 36
	// alert("x:"+xynode[m][0]+"y:"+xynode[m][1]);
	if (clear == 5) {
		clear = 0;
	}
	clear++;
}

// color:1:grey
// color:0:red

function selectOnes() {

}

function set_dot(i, j)// when clicked ,change color of dots
{
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	cxt.fillStyle = "#A9A9A9";
	cxt.beginPath();
	cxt.arc(15 + i * 40, 15 + j * 40, 15, 0, Math.PI * 2, true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.font = "16px Arial";
	cxt.fillText("" + (i * 10 + j), 15 + i * 40 - 7, 15 + j * 40 + 4);
}

function cancel_dot(i, j)// when clicked ,change color of dots
{
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	cxt.fillStyle = "#FF0000";
	cxt.beginPath();
	cxt.arc(15 + i * 40, 15 + j * 40, 15, 0, Math.PI * 2, true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.font = "16px Arial";
	cxt.fillText("" + (i * 10 + j), 15 + i * 40 - 7, 15 + j * 40 + 4);
}
function checkID() {
	if (document.myform.showid.value == "") {
		alert("Please choose a node at least!");
		return false;
	}
	var str = prompt("Please input password", "******");
	if (str != "tinyos") {
		alert("Wrong Password!");
		return false;
	}

	return true;
}

function stateChanged() {
	if (xmlHttp.readyState == 4) {
		// set_dot(6,6);
		// alert(xmlHttp.responseText);
	}
}

function checkblinkfuc(nodestr) {
	var nodestrarray = new Array();
	nodestrarray = nodestr.split(",");
	var count;
	for (count = 0; count < nodestrarray.length; count++) {
		var nodeName = parseInt(nodestrarray[count]); // num from 1~36
		var id = nodeName - 1;
		nodeflagperl[id] = 2;
//		console.log("nodeName:" + nodeName + ",id:" + id + "\n");
	}
}

function blinkfuc1(flag, nodeid) {// flag==1:#ff0000 0:#00ff00
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	var isSink = false;

	var x, y, nodeName;
	if (nodeid == 0) {
		x = parseInt(sink[0]);
		y = parseInt(sink[1]);
		isSink = true;
		nodeName = 1;
		console.log("to blink sink");
	} else {
		var m = nodeid - 1;
		x = parseInt(xynode[m][0]);
		y = parseInt(xynode[m][1]);
		nodeName = parseInt(xynode[m][2]);
	}
	console.log("blinkfuc1:" + isSink + " " + nodeName + "," + x + "," + y);

	if (flag == 1) {
		cxt.fillStyle = "#FF0000";
	} else if (flag == 0) {
		cxt.fillStyle = "#00FF00";
	} else if (flag == 3) {
		cxt.fillStyle = "#00FF00";
	} else if (flag == 4) {
		cxt.fillStyle = "#0000FF";
	}
	cxt.beginPath();
	if (m % 5 == 0 && !isSink) {
		cxt.font = "12px Arial";
		cxt.arc(x, y, 10, 0, Math.PI * 2, true);
	} else {
		cxt.font = "16px Arial";
		cxt.arc(x, y, 16, 0, Math.PI * 2, true);
	}
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.fillText("" + nodeName, x - 8, y + 4);
	console.log("xxx");
}

flag = 0;
function blinkallfuc() {
	flag++;
	var k = 0;
	console.log("blinkallfuc");
	for (k = 0; k < 80; k++) {
		if (nodeflagperl[k] == 2) {
			blinkfuc1(flag % 2, k);
		}
	}
}
function returnstate(responseText) {

	// debug
	/*
	 * var str=null; for(count=0;count<80;count++){
	 * str+=nodeflagperl[count]+","; }
	 */
	// alert(str);
	var responseArray = new Array();
	responseArray = responseText.split(",");
	var count;
	for (count = 0; count < responseArray.length; count += 2) {
		//FIXME: return value is same as textarea from 1~36,while nodeflagperl is from 0~35
		var nodeName = parseInt(responseArray[count]);
		var nodeId = nodeName - 1;
		nodeflagperl[nodeId] = parseInt(responseArray[count + 1]) + 3;
		// change states ,so 0->3,1->4
	}
	// debug
	/*
	 * var str=null; for(count=0;count<80;count++){
	 * str+=nodeflagperl[count]+","; }
	 */
	// alert(str);
}

function showstate() {
	var count = 0;
	for (count = 0; count < 80; count++) {
		if (nodeflagperl[count] == 3) {
			blinkfuc1(3, count);
		} else if (nodeflagperl[count] == 4) {
			blinkfuc1(4, count);
		}
	}
}

function checkIDandOthers() {

	if (document.myform.showid.value == "") {
		alert("Please choose a node at least!");
		return false;
	}
	// var str = prompt("Please input password", "******");
	// if (str != "tinyos") {
	// alert("Wrong Password!");
	// return false;
	// }
	var xmlHttp = new XMLHttpRequest();
	if (xmlHttp == null) {
		alert("no support AJAX！");
		return;
	}
	var url = "burn.jsp";

	var showidvalue = document.myform.showid.value
	url = url + "?showid=" + showidvalue;
	checkblinkfuc(showidvalue);
	Timer = setInterval("blinkallfuc()", 200);
	// set_dot(4,4);
	xmlHttp.onreadystatechange = function() {
		// set_dot(5,5);
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			// set_dot(3,3);
			// alert(xmlHttp.responseText);
			clearInterval(Timer);
			// set_dot(7,7);
			returnstate(xmlHttp.responseText);
			showstate();
		}
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	return true;
}

function show() // show init color
{
	var j;
	for (var i = 0; i < 8; i++) {
		for (j = 0; j < 10; j++) {
			var c = document.getElementById("coordiv");
			var cxt = c.getContext("2d");
			cxt.fillStyle = "#A9A9A9";
			cxt.beginPath();
			cxt.arc(15 + i * 40, 15 + j * 40, 15, 0, Math.PI * 2, true);
			cxt.closePath();
			cxt.fill();
			cxt.fillStyle = "#000000";
			cxt.font = "16px Arial";
			cxt.fillText("" + (i * 10 + j), 15 + i * 40 - 7, 15 + j * 40 + 4);
		}
	}
}

function getNode() {
	// element=document.getElementById('myimage')
	// element.src="pic/downpic.jpg";
	var str = "";
	for (i = 0; i < 80; i++) {
		if (nodeflagperl[i] == 0) {
			str += (i + 1);
			str += ",";
		}
	}
	// document.write(str);

	var c = document.getElementById('showid');
	c.value = str;
}

function cnvs_getCoordinates(e) // when clicked ,get i,j
{
	x = e.clientX - 100;
	y = e.clientY - 80;

	for (var i = 0; i < 8; i++) {
		for (var j = 0; j < 10; j++) {
			tmpx = 15 + 40 * i;
			tmpy = 15 + 40 * j;
			if (((x - tmpx) * (x - tmpx) + (y - tmpy) * (y - tmpy)) <= 15 * 15) {
				var nodeid = i * 10 + j;
				if (nodeflagperl[nodeid] == 1) {
					nodeflagperl[nodeid] = 0;
					cancel_dot(i, j);
				} else if (nodeflagperl[nodeid] == 0) {
					nodeflagperl[nodeid] = 1;
					set_dot(i, j);
				}

				break;
			}
		}

	}
	document.getElementById("xycoordinates1").innerHTML = "Clicked Node ID:"
			+ nodeid;
	// document.getElementById("xycoordinates").innerHTML="Clicked Node ID:::" +
	// nodeid ;
}

function sensor_show(e) // when clicked ,get i,j
{
	x = e.clientX - 100;
	y = e.clientY - 80;

	for (var i = 0; i < 8; i++) {
		for (var j = 0; j < 10; j++) {
			tmpx = 15 + 40 * i;
			tmpy = 15 + 40 * j;
			if (((x - tmpx) * (x - tmpx) + (y - tmpy) * (y - tmpy)) <= 15 * 15) {
				var nodeid = i * 10 + j;
				if (nodeflagperl[nodeid] == 1) {
					nodeflagperl[nodeid] = 0;
					cancel_dot(i, j);
				} else if (nodeflagperl[nodeid] == 0) {
					nodeflagperl[nodeid] = 1;
					set_dot(i, j);
				}

				break;
			}
		}
	}
}
function cnvs_clearCoordinates() // when leave are ,clear x,y
{
	document.getElementById("xycoordinates").innerHTML = "";
}
function printData(e) {

	document.getElementById("xycoordinates").innerHTML = "ccc";
}

function set_dot_tianjing(id, color) {
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	var x = parseInt(xynode[id][0]);
	var y = parseInt(xynode[id][1]);
	var num = parseInt(xynode[id][2]);
	if (color == 1) {
		cxt.fillStyle = "#a9a9a9";
	} else {
		cxt.fillStyle = "#FF0000";
	}
	cxt.beginPath();
	if (id % 5 == 0) {
		cxt.font = "12px Arial";
		cxt.arc(x, y, 10, 0, Math.PI * 2, true);
	} else {
		cxt.font = "16px Arial";
		cxt.arc(x, y, 16, 0, Math.PI * 2, true);
	}
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.fillText("" + num, x - 8, y + 4); // NOTE!: tag no from 1 ~ 36
}

function set_sink(color) {
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	var x = parseInt(sink[0]);
	var y = parseInt(sink[1]);
	var num = 1;
	if (color == 1) {
		cxt.fillStyle = "#a9a9a9";
	} else {
		cxt.fillStyle = "#FF0000";
	}
	cxt.beginPath();
	cxt.font = "16px Arial";
	cxt.arc(x, y, 16, 0, Math.PI * 2, true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.fillText("" + num, x - 8, y + 4);
}

var sink = [ radius + pianyi, canvas_height - 10 ];
function show_tianjing() {
	// nodeid:0
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	cxt.fillStyle = "#a9a9a9";
	cxt.beginPath();
	cxt.arc(sink[0], sink[1], 14, 0, Math.PI * 2, true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.font = "16px Arial";
	cxt.fillText("" + 1, radius + pianyi - 5, canvas_height - 10 + 4);
	// col1:
	for (var m = 0; m < 35; m++) {
		var x = parseInt(xynode[m][0]);
		var y = parseInt(xynode[m][1]);

		var num = parseInt(xynode[m][2]);
		cxt.fillStyle = "#a9a9a9";
		cxt.beginPath();
		if (m % 5 == 0) {
			cxt.font = "12px Arial";
			cxt.arc(x, y, 10, 0, Math.PI * 2, true);
		} else {
			cxt.font = "16px Arial";
			cxt.arc(x, y, 16, 0, Math.PI * 2, true);
		}
		cxt.closePath();
		cxt.fill();
		cxt.fillStyle = "#000000";
		cxt.fillText("" + num, x - 8, y + 4);
	}
}

function isSinkClicked(x, y) {
	console.log("node " + m + " : " + x + "," + y + ";" + sink[0] + ","
			+ sink[1] + "\n");
	console.log("r:" + (Math.pow(x - sink[0], 2) + Math.pow(y - sink[1], 2))
			+ ";" + Math.pow(16, 2) + "\n");
	tmpx = sink[0] - window.pageXOffset;
	tmpy = sink[1] - window.pageYOffset;
	if (tmpx < 0 || tmpy < 0) {
		return false;
	}

	if ((Math.pow(x - tmpx, 2) + Math.pow(y - tmpy, 2)) <= Math.pow(16, 2)) {
		nodeid = 0;
		if (nodeflagperl[nodeid] == 1) {
			set_sink(0);
			nodeflagperl[nodeid] = 0;
		} else if (nodeflagperl[nodeid] == 0) {
			set_sink(1);
			nodeflagperl[nodeid] = 1;
		}
		return true;
	}
	return false;
}

function node_click_tianjing(e) // when clicked ,get i,j
{
	x = e.clientX - 50;
	y = e.clientY - 80;
	var nodeid = -1;
	var tmpradius = 0;
	var c = document.getElementById("main");

	console.log("scroll:" + window.pageXOffset + "," + window.pageYOffset);
	if (isSinkClicked(x, y)) {
		console.log("sink clicked");
	}

	for (var m = 0; m < 35; m++) {
		var tmpx = parseInt(xynode[m][0]) - window.pageXOffset;
		var tmpy = parseInt(xynode[m][1]) - window.pageYOffset;

		// console.log(xynode[m][0] + " " + xynode[m][1] + tmpx + " " + tmpy +
		// "\n");
		if (tmpx < 0 || tmpy < 0) {
			continue;
		}

		var num = parseInt(xynode[m][2]);
		if (m % 5 == 0) {
			tmpradius = 10;
		} else {
			tmpradius = 16;
		}

		if (((x - tmpx) * (x - tmpx) + (y - tmpy) * (y - tmpy)) <= tmpradius
				* tmpradius) {
			nodeid = m + 1;

			if (nodeflagperl[nodeid] == 1) {
				set_dot_tianjing(m, 0);
				nodeflagperl[nodeid] = 0;
			} else if (nodeflagperl[nodeid] == 0) {
				set_dot_tianjing(m, 1);
				nodeflagperl[nodeid] = 1;
			}
			break;
		}
	}
}
